<template>
  <div class="login-container">
    <div class="login-form">
      <div class="form-title">后台管理系统</div>
      <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="0px" class="form-content">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名">
            <el-button slot="prepend"><svg-icon icon-class="people" /></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" @keyup.enter.native="submitForm('loginForm')">
            <el-button slot="prepend"><svg-icon icon-class="lock" /></el-button>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button :loading="loading" type="primary" @click.native.prevent="submitForm">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container{
    position: relative;
    width:100%;
    height:100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.form-title{
    width:100%;
    line-height: 50px;
    text-align: center;
    font-size:20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.login-form{
  position: absolute;
  left:50%;
  top:50%;
  width:350px;
  margin:-190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255,255,255, 0.3);
  overflow: hidden;
}
.form-content{
  padding: 30px 30px;
}
.login-btn{
  text-align: center;
}
.login-btn button{
  width:100%;
  height:36px;
  margin-bottom: 10px;
}
</style>
